home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / VISCAFE.BIN / BasicCell.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-06-19  |  11.3 KB  |  619 lines

  1. package symantec.itools.db.awt;
  2.  
  3. import java.awt.Event;
  4. import java.awt.FontMetrics;
  5. import java.awt.Graphics;
  6. import java.awt.Image;
  7. import java.awt.Rectangle;
  8. import java.awt.image.ImageObserver;
  9.  
  10. public class BasicCell implements TableCell, ImageObserver {
  11.    Grid view;
  12.    DataSource dataSource;
  13.    Coordinate coords;
  14.    boolean selected;
  15.    boolean keyPressedYet;
  16.    boolean loseFocusOnArrow;
  17.    int cursorPos;
  18.    int hlFirst;
  19.    int hlLast;
  20.    boolean selectionMade;
  21.    int offset;
  22.    int chopIndex;
  23.    int toLeftOfCell;
  24.    int startX;
  25.    boolean defaultCell;
  26.    int type;
  27.    static final int PADSIDES = 5;
  28.  
  29.    public BasicCell(Grid var1, DataSource var2) {
  30.       this.view = var1;
  31.       this.dataSource = var2;
  32.    }
  33.  
  34.    public TableCell cloneCell() {
  35.       BasicCell var1 = new BasicCell(this.view, this.dataSource);
  36.       if (this.coords != null) {
  37.          var1.coords = new Coordinate(this.coords.row, this.coords.col);
  38.       }
  39.  
  40.       var1.selected = this.selected;
  41.       var1.keyPressedYet = this.keyPressedYet;
  42.       var1.loseFocusOnArrow = this.loseFocusOnArrow;
  43.       var1.cursorPos = this.cursorPos;
  44.       var1.selectionMade = this.selectionMade;
  45.       var1.offset = this.offset;
  46.       var1.type = this.type;
  47.       return var1;
  48.    }
  49.  
  50.    public int type() {
  51.       return this.type;
  52.    }
  53.  
  54.    public int type(int var1) {
  55.       if (var1 <= 3 && var1 >= 0) {
  56.          return this.type = var1;
  57.       } else {
  58.          throw new IllegalArgumentException("Invalid cell type");
  59.       }
  60.    }
  61.  
  62.    public void setGrid(Grid var1, DataSource var2) {
  63.       this.view = var1;
  64.       this.dataSource = var2;
  65.    }
  66.  
  67.    public void setDefaultFlag() {
  68.       this.defaultCell = true;
  69.    }
  70.  
  71.    public void reset() {
  72.       this.selected = false;
  73.       this.keyPressedYet = false;
  74.       this.loseFocusOnArrow = false;
  75.       this.cursorPos = 0;
  76.       this.selectionMade = false;
  77.       this.offset = 0;
  78.    }
  79.  
  80.    public boolean isCellTypeEditable() {
  81.       return true;
  82.    }
  83.  
  84.    public void setCoordinates(Coordinate var1) {
  85.       this.coords = var1;
  86.    }
  87.  
  88.    public Coordinate getCoordinates() {
  89.       return this.coords;
  90.    }
  91.  
  92.    public int row() {
  93.       return this.coords.row;
  94.    }
  95.  
  96.    public void setRow(int var1) {
  97.       this.coords.row = var1;
  98.    }
  99.  
  100.    public int col() {
  101.       return this.coords.col;
  102.    }
  103.  
  104.    public void setCol(int var1) {
  105.       this.coords.col = var1;
  106.    }
  107.  
  108.    public boolean mouseEvent(Event var1) {
  109.       try {
  110.          switch (var1.id) {
  111.             case 63:
  112.                this.keyPressedYet = false;
  113.                this.view.redrawCell(this);
  114.                this.offset = 0;
  115.                this.view.generateEvent(var1, 63, this);
  116.                break;
  117.             case 501:
  118.                if (!var1.shiftDown()) {
  119.                   this.keyPressedYet = true;
  120.                   this.view.setCapture();
  121.                   this.cursorPos = this.findCursorPos(var1.x);
  122.                   this.selectionMade = false;
  123.                } else {
  124.                   this.hlFirst = this.cursorPos;
  125.                   this.hlLast = this.findCursorPos(var1.x);
  126.                   this.selectionMade = true;
  127.                }
  128.  
  129.                this.view.redrawCell(this);
  130.                this.view.generateEvent(var1, 61, this);
  131.                break;
  132.             case 502:
  133.                this.view.generateEvent(var1, 60, this);
  134.                break;
  135.             case 506:
  136.                if (!this.selectionMade && this.findCursorPos(var1.x) != this.cursorPos) {
  137.                   this.hlFirst = this.cursorPos;
  138.                   this.selectionMade = true;
  139.                }
  140.  
  141.                this.hlLast = this.cursorPos = this.findCursorPos(var1.x);
  142.                this.view.redrawCell(this);
  143.                this.view.generateEvent(var1, 62, this);
  144.          }
  145.       } catch (DataNotAvailable var3) {
  146.          this.view.handleException(this.row(), this.col(), var3);
  147.       }
  148.  
  149.       return true;
  150.    }
  151.  
  152.    int findCursorPos(int var1) throws DataNotAvailable {
  153.       if (var1 < -this.offset) {
  154.          return 0;
  155.       } else {
  156.          int var2 = this.offset + var1;
  157.          FontMetrics var3 = this.view.getCellFontMetrics(this);
  158.          Data var4 = this.readData();
  159.          String var5 = var4.toString();
  160.          int var6 = var5.length();
  161.          Image var7 = var4.toImage();
  162.          int var8 = this.view.getCellAlignment(this);
  163.          if (var8 == 0) {
  164.             if (this.offset == 0) {
  165.                var2 -= 7;
  166.                if (var7 != null) {
  167.                   int var9 = var7.getWidth(this) + 2;
  168.                   int var10 = this.view.getColumnWidth(this.getCoordinates().col);
  169.                   int var11 = var3.stringWidth(var5);
  170.                   if (var9 + var11 + 2 + 5 <= var10) {
  171.                      var2 -= var9;
  172.                   }
  173.                }
  174.             }
  175.          } else if (var8 == 2) {
  176.             int var12 = var3.stringWidth(var5);
  177.             int var15 = this.view.getColumnWidth(this.getCoordinates().col);
  178.             if (var12 <= var15 - 10) {
  179.                var2 = var2 - var15 + var12 + 5;
  180.             }
  181.          } else if (var8 == 1) {
  182.             int var13 = var3.stringWidth(var5);
  183.             int var16 = this.view.getColumnWidth(this.getCoordinates().col);
  184.             if (var13 <= var16) {
  185.                var2 -= (var16 - var13) / 2;
  186.             }
  187.          }
  188.  
  189.          if (var2 <= 0) {
  190.             return 0;
  191.          } else {
  192.             for(int var14 = 1; var14 <= var6; ++var14) {
  193.                if (var3.stringWidth(var5.substring(0, var14 - 1) + var3.charWidth(var5.charAt(var14 - 1)) / 2) > var2) {
  194.                   return var14;
  195.                }
  196.             }
  197.  
  198.             return var6;
  199.          }
  200.       }
  201.    }
  202.  
  203.    public Data getData() throws DataNotAvailable {
  204.       return this.dataSource.getData(this.coords);
  205.    }
  206.  
  207.    public Data readData() throws DataNotAvailable {
  208.       return this.dataSource.readData(this.coords.row, this.coords.col);
  209.    }
  210.  
  211.    void deleteChar(Data var1) {
  212.       if (this.cursorPos != 0) {
  213.          var1.deleteChar(this.cursorPos);
  214.          --this.cursorPos;
  215.       }
  216.    }
  217.  
  218.    void deleteSelection(Data var1) {
  219.       int var2 = Math.min(this.hlFirst, this.hlLast);
  220.       int var3 = Math.max(this.hlFirst, this.hlLast);
  221.  
  222.       for(int var4 = var2; var4 < var3; ++var4) {
  223.          var1.deleteChar(var2 + 1);
  224.       }
  225.  
  226.       this.cursorPos = var2;
  227.    }
  228.  
  229.    public boolean keyEvent(Event var1) {
  230.       Data var2;
  231.       try {
  232.          var2 = this.getData();
  233.       } catch (DataNotAvailable var9) {
  234.          this.view.handleException(this.row(), this.col(), var9);
  235.          return true;
  236.       }
  237.  
  238.       char var3 = (char)var1.key;
  239.       boolean var4 = false;
  240.       boolean var5 = false;
  241.       boolean var6 = var2.isEditable(this.coords.row, this.coords.col) && this.view.getCellEditable(this);
  242.       if (var6 || var1.id != 403 && var1.id != 401) {
  243.          if (!var6 && var1.id == 402) {
  244.             return this.view.generateEvent(var1, 59, this);
  245.          } else if (var1.id == 403) {
  246.             if (var1.shiftDown() && !this.selectionMade) {
  247.                this.selectionMade = true;
  248.                this.hlFirst = this.hlLast = this.cursorPos;
  249.             } else if (!var1.shiftDown()) {
  250.                this.selectionMade = false;
  251.             }
  252.  
  253.             switch (var3) {
  254.                case '╧¿':
  255.                case '╧¼':
  256.                   if (this.cursorPos != 0) {
  257.                      this.cursorPos = 0;
  258.                      var4 = true;
  259.                   }
  260.                   break;
  261.                case '╧⌐':
  262.                case '╧¡':
  263.                   int var7 = var2.toString().length();
  264.                   if (this.cursorPos != var7) {
  265.                      this.cursorPos = var7;
  266.                      var4 = true;
  267.                   }
  268.                case '╧¬':
  269.                case '╧½':
  270.                default:
  271.                   break;
  272.                case '╧«':
  273.                   if (this.cursorPos > 0) {
  274.                      --this.cursorPos;
  275.                      var4 = true;
  276.                   }
  277.                   break;
  278.                case '╧»':
  279.                   if (this.cursorPos < var2.toString().length()) {
  280.                      ++this.cursorPos;
  281.                      var4 = true;
  282.                   }
  283.             }
  284.  
  285.             if (this.selectionMade) {
  286.                this.hlLast = this.cursorPos;
  287.             }
  288.  
  289.             if (var4) {
  290.                this.view.redrawCell(this);
  291.             }
  292.  
  293.             return this.view.generateEvent(var1, 58, this);
  294.          } else {
  295.             if (var1.id == 401) {
  296.                if (!this.keyPressedYet) {
  297.                   if (var1.key < 33 || var1.key > 122) {
  298.                      return this.view.generateEvent(var1, 58, this);
  299.                   }
  300.  
  301.                   var2.clearText();
  302.                   var2.appendChar(var3);
  303.                   this.keyPressedYet = true;
  304.                   this.view.setCapture();
  305.                   this.cursorPos = 1;
  306.                } else {
  307.                   if (this.selectionMade) {
  308.                      this.deleteSelection(var2);
  309.                      this.selectionMade = false;
  310.                      if (var3 == '\b' || var3 == 127) {
  311.                         return this.view.generateEvent(var1, 58, this);
  312.                      }
  313.                   }
  314.  
  315.                   if (var1.key == 27) {
  316.                      this.cursorPos = 0;
  317.                      this.keyPressedYet = false;
  318.                      var5 = this.view.generateEvent(var1, 54, this);
  319.                   } else if (var3 == '\b') {
  320.                      this.deleteChar(var2);
  321.                      var5 = this.view.generateEvent(var1, 57, this);
  322.                   } else if (var3 == '\n') {
  323.                      try {
  324.                         this.dataSource.commitData();
  325.                      } catch (Exception var8) {
  326.                         this.view.handleException(this.row(), this.col(), var8);
  327.                      }
  328.                   } else if (var3 == 127) {
  329.                      if (this.cursorPos != var2.toString().length()) {
  330.                         ++this.cursorPos;
  331.                         this.deleteChar(var2);
  332.                         var5 = this.view.generateEvent(var1, 57, this);
  333.                      }
  334.                   } else if (this.cursorPos == var2.toString().length()) {
  335.                      var2.appendChar(var3);
  336.                      ++this.cursorPos;
  337.                      var5 = this.view.generateEvent(var1, 57, this);
  338.                   } else {
  339.                      var2.insertChar(this.cursorPos, var3);
  340.                      ++this.cursorPos;
  341.                   }
  342.                }
  343.  
  344.                this.view.redrawCell(this);
  345.                var5 |= this.view.generateEvent(var1, 58, this);
  346.             } else if (var1.id == 402) {
  347.                this.view.generateEvent(var1, 59, this);
  348.             }
  349.  
  350.             return var5;
  351.          }
  352.       } else {
  353.          return this.view.generateEvent(var1, 58, this);
  354.       }
  355.    }
  356.  
  357.    public boolean loseFocusOnArrow() {
  358.       return this.selected & !this.keyPressedYet;
  359.    }
  360.  
  361.    public void activateCursor() {
  362.       this.selected = true;
  363.       this.view.redrawCell(this);
  364.    }
  365.  
  366.    public void deactivateCursor() {
  367.       this.selected = false;
  368.       this.view.redrawCell(this);
  369.    }
  370.  
  371.    public boolean canLoseFocus() {
  372.       try {
  373.          this.dataSource.commitData();
  374.          return true;
  375.       } catch (Exception var2) {
  376.          this.view.handleException(this.row(), this.col(), var2);
  377.          return false;
  378.       }
  379.    }
  380.  
  381.    public boolean focusEvent(Event var1) {
  382.       if (var1.id == 1004) {
  383.          this.selected = true;
  384.          this.view.generateEvent(var1, 55, this);
  385.          this.view.redrawCell(this);
  386.       } else {
  387.          this.selected = false;
  388.          this.keyPressedYet = false;
  389.          this.selectionMade = false;
  390.          this.offset = 0;
  391.          this.cursorPos = 0;
  392.          this.view.generateEvent(var1, 56, this);
  393.  
  394.          try {
  395.             this.dataSource.commitData();
  396.          } catch (Exception var3) {
  397.             this.view.handleException(this.row(), this.col(), var3);
  398.          }
  399.  
  400.          this.view.redrawAroundCell(this);
  401.       }
  402.  
  403.       return true;
  404.    }
  405.  
  406.    public boolean actionEvent(Event var1) {
  407.       this.keyPressedYet = false;
  408.       this.view.redrawCell(this);
  409.       this.offset = 0;
  410.       return true;
  411.    }
  412.  
  413.    public void drawCell(Graphics var1, CellHints var2) {
  414.       Object var3;
  415.       try {
  416.          var3 = this.readData();
  417.       } catch (DataNotAvailable var12) {
  418.          this.view.handleException(this.row(), this.col(), var12);
  419.          var3 = new ImageStringData(this.dataSource, "");
  420.       }
  421.  
  422.       Rectangle var4 = var2.bounds();
  423.       FontMetrics var5 = this.view.getCellFontMetrics(this);
  424.       var5.getAscent();
  425.       int var6 = var5.stringWidth(((Data)var3).toString());
  426.       int var7 = 0;
  427.       int var8 = var4.x;
  428.       int var9 = var4.width;
  429.       var2.setBackground(var1);
  430.       var1.fillRect(var4.x, var4.y, var4.width - 1, var4.height - 1);
  431.       Image var10 = ((Data)var3).toImage();
  432.       switch (var2.alignment()) {
  433.          case 0:
  434.             if (var10 != null) {
  435.                var7 = var10.getWidth(this) + 2;
  436.             }
  437.  
  438.             if (var7 + var6 + 2 + 5 <= var4.width && var10 != null) {
  439.                var4.x += 5;
  440.                ++var4.y;
  441.                var1.drawImage(var10, var4.x, var4.y, this);
  442.                --var4.y;
  443.             } else {
  444.                var7 = 0;
  445.             }
  446.  
  447.             var4.x = var8 + var7 + 5;
  448.             break;
  449.          case 1:
  450.             if (var6 > var4.width - 10) {
  451.                var4.x += 5;
  452.             } else {
  453.                var4.x += (var4.width - var6) / 2;
  454.             }
  455.             break;
  456.          case 2:
  457.             if (var10 != null) {
  458.                var7 = var10.getWidth(this);
  459.             }
  460.  
  461.             if (var7 + 2 + var6 + 10 + 3 <= var4.width && var10 != null) {
  462.                var4.x = var4.x + var4.width - var6 - var7 - 2 - 5 - 3;
  463.                ++var4.y;
  464.                var1.drawImage(var10, var4.x, var4.y, this);
  465.                --var4.y;
  466.                var4.x = var8 + var4.width - var6 - 5 - 3;
  467.             } else {
  468.                var7 = 0;
  469.                if (var6 > var4.width - 10) {
  470.                   var4.x += 5;
  471.                } else {
  472.                   var4.x = var8 + var4.width - var6 - 5 - 3;
  473.                }
  474.             }
  475.       }
  476.  
  477.       var2.setForeground(var1);
  478.       var4.width = var4.width + var8 - var4.x - 5;
  479.       this.startX = var4.x;
  480.       this.drawText((Data)var3, var1, var4, var5, var2);
  481.       var4.width = var9;
  482.       if (this.selected && this.keyPressedYet) {
  483.          int var11 = 0;
  484.          if (((Data)var3).toString().length() > 0) {
  485.             var11 = var5.stringWidth(((Data)var3).subString(this.toLeftOfCell, this.cursorPos));
  486.          }
  487.  
  488.          var1.drawLine(var4.x + var11, var4.y + 2, var4.x + var11, var4.y + var5.getHeight());
  489.       }
  490.  
  491.       var4.x = var8;
  492.       var2.drawBoundary(var1);
  493.    }
  494.  
  495.    protected String chopString(Data var1, Rectangle var2, FontMetrics var3) {
  496.       String var4 = var1.toString();
  497.       int var5 = this.view.getColumnWidth(this.coords.col);
  498.       this.chopIndex = 1;
  499.       if (var4.length() == 0) {
  500.          return var4;
  501.       } else {
  502.          this.setOffset(var4, var2, var3);
  503.          if (this.offset == 0) {
  504.             this.toLeftOfCell = 0;
  505.          } else {
  506.             while(this.toLeftOfCell < var4.length()) {
  507.                int var7 = var3.stringWidth(var4.substring(0, this.toLeftOfCell));
  508.                if (var7 >= this.offset + 5) {
  509.                   --this.toLeftOfCell;
  510.                   break;
  511.                }
  512.  
  513.                ++this.toLeftOfCell;
  514.             }
  515.  
  516.             this.toLeftOfCell -= this.toLeftOfCell < var4.length() ? 0 : 1;
  517.             var4 = var4.substring(this.toLeftOfCell, var4.length());
  518.          }
  519.  
  520.          String var6;
  521.          do {
  522.             var6 = var4.substring(0, this.chopIndex);
  523.          } while(var3.stringWidth(var6) < var5 - 10 && this.chopIndex++ < var4.length());
  524.  
  525.          return var4.substring(0, --this.chopIndex);
  526.       }
  527.    }
  528.  
  529.    protected void drawText(Data var1, Graphics var2, Rectangle var3, FontMetrics var4, CellHints var5) {
  530.       String var6 = this.chopString(var1, var3, var4);
  531.       if (this.selected && !this.keyPressedYet) {
  532.          var5.setForeground(var2);
  533.          var2.fillRect(var3.x - 1, var3.y + 3, var4.stringWidth(var6.toString()) + 4, var4.getHeight() - 1);
  534.          var5.setBackground(var2);
  535.          var2.drawString(var6, var3.x, var3.y + var4.getAscent() + 2);
  536.       } else if (this.selected && !this.selectionMade) {
  537.          var2.drawString(var6, var3.x, var3.y + var4.getAscent() + 2);
  538.       } else if (this.selected && this.selectionMade) {
  539.          int var7 = Math.min(this.hlFirst, this.hlLast);
  540.          int var8 = Math.max(this.hlFirst, this.hlLast);
  541.          var8 = Math.min(var8, this.chopIndex);
  542.          if (this.toLeftOfCell != 0) {
  543.             var7 = Math.max(this.hlFirst, this.toLeftOfCell);
  544.          }
  545.  
  546.          String var9 = var6.substring(0, var7);
  547.          var2.drawString(var9, var3.x, var3.y + var4.getAscent() + 2);
  548.          int var10 = var4.stringWidth(var9) + var3.x - this.offset;
  549.          var9 = var6.substring(var7, var8);
  550.          int var11 = var4.stringWidth(var9) + var10;
  551.          var5.setForeground(var2);
  552.          var2.fillRect(var10, var3.y + 2, var11 - var10, var4.getHeight() - 1);
  553.          var5.setBackground(var2);
  554.          var2.drawString(var9, var10, var3.y + var4.getAscent() + 2);
  555.          var5.setForeground(var2);
  556.          var9 = var6.substring(var8, var6.length());
  557.          var2.drawString(var9, var11, var3.y + var4.getAscent() + 2);
  558.       } else {
  559.          var2.drawString(var6, var3.x, var3.y + var4.getAscent() + 2);
  560.       }
  561.    }
  562.  
  563.    void setOffset(String var1, Rectangle var2, FontMetrics var3) {
  564.       String var4 = var1.substring(0, this.cursorPos);
  565.       int var5 = var3.stringWidth(var4);
  566.       byte var6 = 3;
  567.       if (!this.selected) {
  568.          this.offset = 0;
  569.       } else if (var5 > var2.width - var6) {
  570.          this.offset = -var2.width + var5 + var6;
  571.       } else {
  572.          this.offset = 0;
  573.       }
  574.    }
  575.  
  576.    int textLeft(int var1, Rectangle var2) {
  577.       switch (this.view.getCellAlignment(this)) {
  578.          case 0:
  579.          default:
  580.             return var2.x + 5;
  581.          case 1:
  582.             if (var1 > var2.width) {
  583.                return var2.x;
  584.             }
  585.  
  586.             return var2.x + (var2.width - var1) / 2;
  587.          case 2:
  588.             return var1 > var2.width ? var2.x + 5 : var2.x + var2.width - var1 - 5;
  589.       }
  590.    }
  591.  
  592.    public boolean imageUpdate(Image var1, int var2, int var3, int var4, int var5, int var6) {
  593.       if ((var2 & 192) != 0) {
  594.          return false;
  595.       } else if ((var2 & 32) != 0) {
  596.          this.view.redrawAsync();
  597.          return false;
  598.       } else {
  599.          return true;
  600.       }
  601.    }
  602.  
  603.    public String toString() {
  604.       try {
  605.          return this.readData().toString();
  606.       } catch (DataNotAvailable var1) {
  607.          return "ERROR getting data";
  608.       }
  609.    }
  610.  
  611.    public String stats() {
  612.       try {
  613.          return "BasicCell: row=" + this.coords.row + " col=" + this.coords.col + " text=" + this.readData().toString();
  614.       } catch (DataNotAvailable var1) {
  615.          return "BasicCell: row=" + this.coords.row + " col=" + this.coords.col + " could not retrieve data";
  616.       }
  617.    }
  618. }
  619.